<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/27 0027
 * Time: 21:51
 */
class Thread2 extends Thread
{
    public $hooks = array();

    public $args = array();

    public function thread()
    {
    }

    public function addthread($func)
    {
        $args = array_slice(func_get_args(), 1);
        $this->hooks = $func;
        $this->args = $args;
        return true;
    }

    public function runthread()
    {
        if (isset($_GET['flog'])) $flog = intval($_GET['flog']);
        if (isset($flog) || $flog === 0) {
            call_user_func_array($this->hooks, $this->args);
        } else {
            for ($i = 0, $size = count($this->hooks); $i < $size; $i++) {
                $fp = fsockopen($_SERVER['HTTP_HOST'], $_SERVER['HTTP_PORT']);
                if ($fp) {
                    $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1\r\n";
                    $out .= "Host: {$_SERVER['HTTP_HOST']}\r\n";
                    $out .= "Connection: Closernrn";
                    fputs($fp, $out);
                    fclose($fp);
                }
            }
        }

    }
}

class asyncOperation extends Thread2
{
    public function __construct($args)
    {
        $this->args = $args;
    }

    public function run()
    {
        if ($this->arg) {
            printf("Hello %s\n", $this->arg);
        }
    }

}
$thread = new AsyncOperation("World");
if($thread->start())
    $thread->join();